<template>
  <div class="elevator-container">
    <!-- 图例区域 -->
    <div class="legend-container">
      <div
        class="item-container"
        v-for="(item, index) in typeList"
        :key="index"
      >
        <div class="item-icon" :style="{ backgroundColor: item.color }"></div>
        <div class="item-text">{{ item.name }}</div>
      </div>
    </div>

    <!-- 运行图区域 -->
    <div class="charts-container">
      <div class="item-container" v-for="item in elevatorList" :key="item.id">
        <div class="item-bar-container" ref="barContainerRef">
          <div
            class="bar-block-container"
            :ref="`${item.id}Ref`"
            :style="{ '--duration': duration }"
          >
            <div class="block-desc">{{ item.positionArr[item.position] }}</div>
            <div
              class="block"
              :style="{ backgroundColor: typeList[item.type].color }"
            ></div>
          </div>
        </div>
        <div class="item-name">{{ item.name }}</div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: "ElevatorOperation",
  props: {
    data: {
      type: Array,
      default: () => []
    },
    duration: {
      type: String,
      default: "2.5s"
    }
  },
  data() {
    return {
      typeList: [
        {
          name: "电梯上行",
          color: "rgba(39, 166, 255, 1)"
        },
        {
          name: "电梯下行",
          color: "rgba(135, 205, 255, 1)"
        },
        {
          name: "电梯悬停",
          color: "rgba(255, 255, 255, 1)"
        },
        {
          name: "电梯故障",
          color: "rgba(255, 116, 76, 1)"
        }
      ],
      elevatorList: []
    };
  },

  watch: {
    data: {
      handler(newVal) {
        this.elevatorList = newVal;
        if (!newVal.length) return;
        this.$nextTick(() => this.setElevatorPositionAnimation(newVal));
      },
      immediate: true
    }
  },

  methods: {
    /**
     * 设置电梯位置动画方法
     * @param {Array<Object>} data 传入电梯相关数据
     */
    setElevatorPositionAnimation(data) {
      const barContainerHeight = this.$refs.barContainerRef[0].offsetHeight;
      data.forEach(item => {
        const { id, positionArr, position } = item;
        const barDom = this.$refs[`${id}Ref`][0];
        const barHeight = barDom.offsetHeight;
        const step = (barContainerHeight - barHeight / 2) / positionArr.length;
        const offset = step * position;
        barDom.style.transform = `translateY(-${offset}px)`;
      });
    }
  }
};
</script>

<style lang="less" scoped>
@duration: var(--duration);

.elevator-container {
  display: flex;
  flex-direction: column;
  width: 100%;
  height: 100%;

  // 图例区域样式
  .legend-container {
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    margin-top: 1.25rem;

    .item-container {
      display: flex;
      align-items: center;

      .item-icon {
        width: 0.75rem;
        height: 0.75rem;
        background: #27a6ff;
      }

      .item-text {
        margin-left: 0.625rem;
        font-size: 0.875rem;
        font-family: HarmonyOS Sans SC, HarmonyOS Sans SC;
        font-weight: 500;
        color: #fff;
        line-height: 1rem;
      }
    }
  }

  // 运行图区域样式
  .charts-container {
    display: flex;
    align-items: center;
    justify-content: space-around;
    flex: 1;
    margin-top: 1.25rem;

    .item-container {
      display: flex;
      flex-direction: column;
      align-items: center;
      height: 100%;

      .item-bar-container {
        position: relative;
        width: 0.875rem;
        height: 100%;
        background: #164973;
        border-radius: 0.0625rem;
        border: 0.0625rem solid rgba(176, 224, 255, 0.5);

        .bar-block-container {
          position: absolute;
          right: 0;
          bottom: 0;
          display: flex;
          align-items: center;
          height: 0.8125rem;
          transition: transform @duration;

          .block-desc {
            margin-right: 0.25rem;
            flex-shrink: 0;
            font-size: 0.75rem;
            font-family: HarmonyOS Sans SC, HarmonyOS Sans SC;
            font-weight: 500;
            color: #fff;
            line-height: 0.875rem;
          }

          .block {
            flex-shrink: 0;
            width: 0.875rem;
            height: 0.8125rem;
          }
        }
      }

      .item-name {
        margin-top: 0.3125rem;
        font-size: 0.875rem;
        font-family: HarmonyOS Sans SC, HarmonyOS Sans SC;
        font-weight: 500;
        color: #ffffff;
        line-height: 1rem;
      }
    }
  }
}
</style>
